from django.db import models
from django.contrib import admin


class User(models.Model):
    username = models.TextField()
    nickname = models.TextField()


class Author(models.Model):
    name = models.CharField(max_length=80)
    age = models.IntegerField(default=0)
    sex = models.CharField(max_length=10)
    email = models.EmailField()

    def __unicode__(self):
        return self.name


class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField(default='')
    attach = models.FileField(upload_to="datasite/blog/file/")
    pub_time = models.DateTimeField('PUB_TIME')
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

    def __unicode__(self):
        return self.title


class ArticleComment(models.Model):
    content = models.TextField()
    pub_time = models.DateTimeField('PUB_TIME')
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    article = models.ForeignKey(Article, on_delete=models.CASCADE)

    def __unicode__(self):
        return self.content


class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'content', 'attach', 'pub_time')


class AuthorAdmin(admin.ModelAdmin):
    list_display = ('name', 'age', 'sex', 'email')


class UserAdmin(admin.ModelAdmin):
    list_display = ('username', 'nickname')


admin.site.register(User, UserAdmin)
admin.site.register(Article, ArticleAdmin)
admin.site.register(Author, AuthorAdmin)
